package com.example.studentmanagement.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.studentmanagement.StudentListActivity;
import com.example.studentmanagement.entity.Student;
import com.example.studentmanagement.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class StudentDao {

    public static SQLiteDatabase db=DBUtil.db;

    @SuppressLint("Range")
    public static List<Student> getAllStudent(){
        List<Student> students = new ArrayList<>();
        Cursor cursor = db.query("d_student" , new String[]{"s_id" , "s_pwd" , "s_name" , "s_pro" ,"s_sex" } ,
                null , null , null , null ,null);
        if (cursor != null && cursor.moveToFirst()){
            do {
                String s_id = cursor.getString(cursor.getColumnIndex("s_id"));
                String s_pwd = cursor.getString(cursor.getColumnIndex("s_pwd"));
                String s_name = cursor.getString(cursor.getColumnIndex("s_name"));
                String s_pro = cursor.getString(cursor.getColumnIndex("s_pro"));
                String s_sex = cursor.getString(cursor.getColumnIndex("s_sex"));
                students.add(new Student(s_id, s_pwd , s_name , s_pro ,s_sex));
                Log.d("TAG", "getAllStudent: #####################");
            }while (cursor.moveToNext());
        }
        if (cursor != null){
            cursor.close();
        }
//        db.close();

        return students ;
    }

    @SuppressLint("Range")
    public Student getStudentById(String studentId){

        String query = "SELECT s_pwd , s_name , s_pro , s_sex FROM d_student WHERE s_id = ?" ;
        Cursor cursor = db.rawQuery(query , new String[]{String.valueOf(studentId)});
        Student student = null ;
        if (cursor.moveToFirst()){
            String s_pwd = cursor.getString(cursor.getColumnIndexOrThrow("s_pwd"));
            String s_name = cursor.getString(cursor.getColumnIndexOrThrow("s_name"));
            String s_pro = cursor.getString(cursor.getColumnIndexOrThrow("s_pro"));
            String s_sex = cursor.getString(cursor.getColumnIndexOrThrow("s_sex"));
            student = new Student(s_pwd, s_name, s_pro, s_sex);
        }
        cursor.close();
        return student ;
    }

    public boolean deleteStudent(String studentId) {
        String whereClause = "s_id = ?";
        String[] whereArgs = { studentId };
        int rowsDeleted = db.delete("d_student", whereClause, whereArgs);
//        db.close();

        return rowsDeleted >0 ;
    }

    public long insertStudent(Student student){

        ContentValues contentValues = new ContentValues() ;
        contentValues.put("s_id" ,student.getUserid() );
        contentValues.put("s_pwd" , student.getPwd());
        contentValues.put("s_name" , student.getName());
        contentValues.put("s_pro" , student.getPro());
        contentValues.put("s_sex" , student.getSex());
        long students = db.insert("d_student" ,null , contentValues) ;
        return students ;
    }

    public boolean isStudentEnrolled(String s_id) {
        boolean isEnrolled = false;

        // 假设choose表用于关联student和course
        String query = "SELECT s_pro FROM d_student WHERE s_id = ? AND s_pro = 'GL000'";
        String[] queryArgs = { s_id };

        Cursor cursor = null;
        try {
            cursor = db.rawQuery(query, queryArgs);
            isEnrolled = cursor.getCount() > 0; // 如果游标中有数据，说明已选课
        } catch (Exception e) {
            // 异常处理
        } finally {
            if (cursor != null) {
                cursor.close(); // 关闭游标释放资源
            }
//            db.close(); // 关闭数据库
        }

        return isEnrolled;
    }
    public String getPasswordByStudentID(String studentId){
        String query = "SELECT s_pwd FROM d_student WHERE s_id = ?";
        Cursor cursor = db.rawQuery(query,new String[]{studentId});
        if (cursor !=null && cursor.moveToFirst()){
            @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex("password"));
            cursor.close();
            return password ;
        }
        return null ;
    }

    public boolean updatePassword(String studentId, String newPassword) {
        ContentValues values = new ContentValues();
        values.put("password", newPassword);
        int rowsAffected = db.update("d_student", values, "s_id = ?", new String[]{studentId});
        return rowsAffected > 0;
    }

}
